package ru.zombator.taskman.server.query;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import ru.zombator.taskman.core.query.LogHandler;

public class Logger {
	private List<LogHandler> handlers = new ArrayList<LogHandler>();
	
	public void addHandler(LogHandler handler) {
		handlers.add(handler);
	}
	
	public void removeHandler(LogHandler handler) {
		handlers.remove(handler);
	}
	
	public void log(String what) {
		final String DATE_FORMAT_NOW = "yyyyMMdd HH:mm:ss";
	    Calendar cal = Calendar.getInstance();
	    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
	    final String nowString = sdf.format(cal.getTime());
		
		for (LogHandler handler: handlers) {
			handler.logString(nowString + "  " + what);
		}
	}
	
	public void log(String formatString, Object... params) {
		log(String.format(formatString, params));
	}	
}
